<?php

namespace app\api\controller;

use app\admin\model\CouponRecord;
use app\admin\model\Details;
use app\admin\model\Products;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Request;
use think\Validate;

/**
 * 优惠券
 */
class Coupon extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /*
   * 下单选择优惠券列表
   */
    public function order_coupon_record(){

        if (!$id = input('good_id/d')) {
            $this->error(__('请选择服务'));
        }

        if (input('type/d')===null) {
            $this->error(__('请选择类型'));
        }
        if(input('type')==0){
            $products = Products::get($id);
        }else{
            $products = Details::get($id);
        }
        $money = $products['price'];
        $list = CouponRecord::field('id,name,ROUND(money) AS money,min_money,expire_time,status,createtime,coupon_type,start_time')
            ->where('status', 0) //
            ->where('type', 'FULL')
            ->where('user_id',  $this->auth->id)
            ->where('start_time','<',  time())
            ->where('min_money', 'elt',$money)
            ->order('id desc')->select();
        foreach ($list as $item) {
            $item['expire_time_text']=date('Y.m.d', $item['start_time']).'-'.date('Y.m.d', $item['expire_time']);
        }
        $this->success('请求成功', $list);
    }

    /*
 * 优惠券列表
 */
    public function coupon_record_list(){
        $status = input('status');
        $query = CouponRecord::where(function($query) use ($status) {
            if($status!==null&&$status!==""){
                $query->where('status', $status);
            }
        });
        $query->where('start_time','<',time());
        $query->where('user_id', $this->auth->id);
        $list = $query->field('id,name,ROUND(money,2) AS money,min_money,expire_time,status,createtime,start_time')->order('id desc')->paginate(10);

        foreach ($list as $item) {
            $item['expire_time_text']=date('Y.m.d', $item['start_time']).'-'.date('Y.m.d', $item['expire_time']);
        }

        $this->success('请求成功', $list);

    }



}
